<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="3" :xs="24">
        <div class="head-container">
            <el-button
              plain
              icon="el-icon-sort"
              size="mini"
              @click="toggleExpandAll"
            >展开/折叠</el-button>
        </div>
        <div class="head-container">
          <el-tree
            v-if="refreshTable"
            :data="typeOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :default-expand-all="isExpandAll"
            :filter-node-method="filterNode"
            ref="tree"
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="21" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="物料号" prop="materialNum">
            <el-input
              v-model="queryParams.materialNum"
              placeholder="请输入物料号"
              style="width: 150px"
            />
          </el-form-item>
          <el-form-item label="物料名称" prop="materialName">
            <el-input
              v-model="queryParams.materialName"
              placeholder="请输入物料名称"
              style="width: 150px"
            />
          </el-form-item>
          <el-form-item label="规格型号" prop="materialSpecs">
            <el-input
              v-model="queryParams.materialSpecs"
              placeholder="请输入规格型号"
              style="width: 150px"
            />
          </el-form-item>
          <el-form-item label="本安认证" prop="isSafe">
            <el-select
              v-model="queryParams.isSafe"
              style="width: 100px"
            >
              <el-option
                v-for="dict in dict.type.mc_yes_no"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
          <el-form-item style="position: absolute; right: 25px">
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['mc:material:mgr']"
              >新增</el-button>
            </el-col>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="userList" >
          <el-table-column label="ERP物料号"  prop="materialNum" width="120"/>
          <el-table-column label="物料名称" prop="materialName" min-width="170"   />
          <el-table-column label="是否本安" align="center" prop="isSafe" width="80">
            <template slot-scope="scope">
              <span>{{scope.row.isSafe ? '是':'否'}}</span>
            </template>
          </el-table-column>
          <el-table-column label="协议类型" align="center" prop="protocolType" width="100">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.mc_protocol_type"
                :value="scope.row.protocolType"
              />
            </template>
          </el-table-column>
          <el-table-column label="规格型号" align="center" prop="materialSpecs"    />
          <el-table-column label="ERP软件版本编码" align="center" prop="softVerCode"  />
          <el-table-column label="ERP软件版本名称" align="center" prop="softVerName"  >
            <template slot-scope="scope">
              <div style="white-space: pre-line;">{{ scope.row.softVerName }}</div>
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="materialRemark"  width="100"/>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope" >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['mc:material:mgr']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['mc:material:mgr']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改物料信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="auto" :rules="dynamicRules" >
        <el-row>
        <el-col :span="24">
          <el-form-item label="物料类型" prop="materialType" v-if="form.type === 'add'">
            <treeselect v-model="form.materialType" :options="typeOptions" placeholder="选择物料类型" />
          </el-form-item>
        </el-col>
       </el-row>
        <el-row>
          <el-col :span="15">
            <el-form-item label="ERP物料号" prop="materialNum" >
              <el-input v-model="form.materialNum" placeholder="请输入物料号" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item label="是否本安">
              <el-select v-model="form.isSafe" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.mc_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="物料名称" prop="materialName" >
              <el-input v-model="form.materialName" placeholder="请输入物料名称" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="14">
            <el-form-item label="规格型号" prop="materialSpecs" >
              <el-input v-model="form.materialSpecs" placeholder="请输入规格型号" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="协议类型" prop="protocolType" v-if="form.type !== 'add'" >
            <el-select v-model="form.protocolType" placeholder="请选择">
              <el-option
                v-for="dict in dict.type.mc_protocol_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"></el-option>
            </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="ERP软件版本编码"  >
              <el-input v-model="form.softVerCode" placeholder="请输入ERP软件版本编码" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="ERP软件版本名称" >
              <el-input v-model="form.softVerName" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" >
              <el-input v-model="form.materialRemark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="14">
            <el-form-item label="发送邮件通知" prop="isEmail">
              <el-select v-model="form.isEmail" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.mc_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10" v-if="form.type === 'modify'">
            <el-form-item label="是否隐藏" prop="hideFlag">
              <el-select v-model="form.hideFlag" placeholder="请选择" >
                <el-option
                  v-for="dict in dict.type.mc_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {materialTreeSelect, listMaterial, delMaterial, addMaterial, updateMaterial} from "@/api/material/material"
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "MaterialQuery",
  dicts: ['mc_protocol_type', 'mc_yes_no'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: "",
      // 物料类别树选项
      typeOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 日期范围
      dateRange: [],
      // 岗位选项
      postOptions: [],
      // 角色选项
      roleOptions: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialNum: undefined,
        materialName: undefined,
        materialSpecs:undefined,
        materialType: 100,
        isSafe: undefined
      },
      modifyRules: {
        materialNum:[
          { required: false, message: "物料号不能为空", trigger: "blur" }
        ],
        materialType:[
          { required: false, message: "物料类型不能为空", trigger: "blur" }
        ]
      },
      addRules: {
        materialNum:[
          { required: true, message: "物料号不能为空", trigger: "blur" }
        ],
        materialType:[
          { required: true, message: "物料类型不能为空", trigger: "blur" }
        ]
      }
    };
  },
  watch: {

  },
  created() {
    this.getList();
    this.getTypeTree();
  },
  computed: {
    dynamicRules() {
      return this.form.type === 'modify' ? this.modifyRules : this.addRules;
    }
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listMaterial(this.queryParams).then(response => {
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 查询部门下拉树结构 */
    getTypeTree() {
      materialTreeSelect().then(response => {
        this.typeOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      console.log(value,data);
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      if(this.isExpandAll){
        this.getTypeTree();
      }
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.materialType = data.id;
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.typeOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        type : null,
        materialNum: null,
        isEmail: '1',
        hideFlag: '0'
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.type = 'add';
      this.form.materialType = (100 == this.queryParams.materialType ? 102: this.queryParams.materialType);
      this.form.isSafe = '0';
      this.open = true;
      this.title = "添加物料信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form.type = 'modify';
      this.form.id = row.id;
      this.open = true;
      this.title = "修改物料信息";
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.type === 'modify') {
            updateMaterial(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaterial(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const materialId = row.id;
      this.$modal.confirm('是否确认删除物料号为"' + row.materialNum + '"的数据项？').then(function() {
        return delMaterial(materialId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
<style scoped>
.head-container {
  margin-bottom: 20px; /* 垂直间距 */
}
</style>
